<?php
//获取一个随机且唯一的编号；
function getNumber($user_level){
    $m_user = new \app\home\model\User();
    $level = $m_user->level($user_level,'prefix');
    $one=$m_user->where(array('number'=>array('like','%'.$level['prefix'].'%')))->order('add_time desc')->getField('number');
    $str_n = '';
    if($one){
        $str_l=(substr($one,-6)+1);
        for($i=6;$i>strlen($str_l);$i--){
            $str_n .= '0';
        }
        $NO=$level['prefix'].$str_n.$str_l;
    }else{
        $NO = $level['prefix'].'000001';
    }
    return $NO;
}

/**
 * 获取某年第几周的开始日期和结束日期
 * @param int $start 开始时间
 * @param int $end 结束时间;
 * @param int $type 返回类型;
 */
function weekday($start,$end,$type = 0){
    $time = $end - $start;
    $m = 86400*7;//七天秒数
    $s = 86400*6;
    $week = ceil($time/$m);
    $weekday = [];
    for ($i=1;$i<=$week;$i++){
        $weekday[$i]['start'] = $start+($m*($i-1));
        $weekday[$i]['end'] = $weekday[$i]['start']+$s;
    }
    if($type == 3){
        return array('week'=>$week,'weekday'=>$weekday);
    }

    if($type == 0){
        $start = date('Y.m.d',$weekday[$week]['start']);
        $end = date('Y.m.d',$weekday[$week]['end']);
        $string = '第'.$week.'周<span class="gray">（'.$start.'-'.$end.'）</span>';
    }elseif ($type == 1){
        $start = date('Y.m.d',$weekday[$week]['start']);
        $end = date('Y.m.d',$weekday[$week]['end']);
        $string = '第'.$week.'周（'.$start.'-'.$end.'）';
    }

    return $string;
}
/**
 * 返回该人的业绩统计
 * @param int $user_id 用户ID
 * @param int $week 第几周;
 */
function achievement($user_id,$s_end,$arr_id=array(),$type = 0){
    $where['type'] = array('in','1,2,3,4');
    $where['user_id'] = $user_id;
    $lists = M('user_erecord')->where($where)->select();
    $achieve = array(
        'end' => 0,
        'no'  => 0,
    );
    $team = [];
    foreach ($lists as $k=>$v){
        if($v['addtime'] > $s_end['start'] && $v['addtime'] < $s_end['end']){
            if($v['status'] == 0){
                $achieve['end'] += $v['price'];
                $team[$v['from_id']]['end'] += $v['price'];
            }
            if($type == 3){
                if($v['type'] == 1){
                    $team[$v['from_id']]['tj'] += $v['price'];
                }else if($v['type'] == 2){
                    $team[$v['from_id']]['fh'] += $v['price'];
                }else if($v['type'] == 3){
                    $team[$v['from_id']]['ls'] += $v['price'];
                }else if($v['type'] == 4){
                    $team[$v['from_id']]['ld'] += $v['price'];
                }
            }
        }
        if($v['status'] == 0){
            $achieve['no'] += $v['price'];
        }
        $achieve['count'] += $v['price'];
        $team[$v['from_id']]['count'] += $v['price'];
    }
    if($type == 2 || $type == 3){
        $price = array();
        foreach ($team as $k=>$v){
            if(in_array($k,$arr_id)){
                $price['end'] += $v['end'];
                $price['count'] += $v['count'];
                if($type == 3){
                    $price['tj'] += $v['tj'];
                    $price['fh'] += $v['fh'];
                    $price['ls'] += $v['ls'];
                    $price['ld'] += $v['ld'];
                }
            }
        }
        return $price;
    }

    return $achieve;
}



